package com.huikeportal.controller;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.huike.model.MsgResponse;
import com.huike.tools.DateTool;
import com.huike.tools.MessageTool;
import com.huikeportal.model.ChangePwd;
import com.huikeportal.model.MessageCommon;
import com.huikeportal.model.common.InterFaceModel;
import com.huikeportal.service.ChangePwdService;
import com.huikeportal.service.InterFaceCommon;

@Controller
public class ChangePwdAPI {

	@Autowired
	private ChangePwdService changePwdService;

	@Autowired
	private InterFaceCommon interFaceCommon;

	/**
	 * 修改密码接口
	 * 
	 * @param userId
	 *            用户编号
	 * @param oldPwd
	 *            原密码
	 * @param newPwd
	 *            新密码
	 * @return
	 */
	@RequestMapping(value = "/huike_changePwd", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String changePwd(String userId, String oldPwd, String newPwd) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_changePwd");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (userId == null || oldPwd == null || newPwd == null
				|| "".equals(userId) || "".equals(oldPwd) || "".equals(newPwd)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		// 处理
		try {
			ChangePwd changePwd = new ChangePwd();
			changePwd.setOldPassword(oldPwd);
			changePwd.setNewPassword(newPwd);
			changePwd.setUserId(userId);
			// 更新密码
			MessageCommon messcomm = changePwdService.updatePwd(changePwd);
			// 如果更新失败
			if (!messcomm.isFlag()) {
				msgResponse.setRespCode("960001");
				msgResponse.setRespCodeDes(MessageTool.RESP_960001);
				interFaceModel.setEndTime(DateTool.getDateYmdHms());
				interFaceModel.setInterStatus("success");
				interFaceCommon.logInterface(interFaceModel);
				return JSONObject.fromObject(msgResponse).toString();
			}
			// 成功
			else {
				msgResponse.setRespCode("90001");
				msgResponse.setRespCodeDes(MessageTool.RESP_90001);
				interFaceModel.setEndTime(DateTool.getDateYmdHms());
				interFaceModel.setInterStatus("success");
				interFaceCommon.logInterface(interFaceModel);
				return JSONObject.fromObject(msgResponse).toString();
			}
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}
}
